package com.srwl.mytx.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.android.material.snackbar.Snackbar;
import com.srwl.mytx.domain.Contact;
import com.srwl.mytx.Helper;
import com.srwl.mytx.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dahai2070 on 2018/5/18 0018.
 */

public class GroupDialog extends AlertDialog {
    private Context context;
    private Contact contact;
    private RelativeLayout rl_confirm;
    private EditText et_creatGroup;
    private ListView listView;
    private InteractionListener listener;

    public GroupDialog(Context context) {
        super(context);
        this.context = context;
    }

    public GroupDialog(Context context, Contact contact) {
        super(context);
        this.context = context;
        this.contact = contact;
    }

    public GroupDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public GroupDialog(Context context, int themeResId) {
        super(context, themeResId);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        final List<String> groupList = new ArrayList<String>();
        List<Contact> contactList = Helper.getInstance().getContactList();
        for (Contact contact : contactList) {
            if (!groupList.contains(contact.getGroupName())) {
                groupList.add(contact.getGroupName());
            }
        }
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        setContentView(R.layout.group_dialog);
        listView = findViewById(R.id.list_group);
        rl_confirm = findViewById(R.id.rl_confirm);
        findViewById(R.id.rl_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        et_creatGroup = findViewById(R.id.et_create_group);
        if (contact != null) {
            et_creatGroup.setText(contact.getGroupName());
            et_creatGroup.setSelection(contact.getGroupName().length());
        }

        rl_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String groupName = et_creatGroup.getText().toString().trim();
                if (TextUtils.isEmpty(groupName)) {
                    //Snackbar.make(rl_confirm, "分组名称不能为空", Snackbar.LENGTH_LONG).show();
                    groupName = et_creatGroup.getHint().toString();

                }
                if (contact != null && TextUtils.equals(contact.getGroupName(), groupName)) {
                    Snackbar.make(rl_confirm, "已在该分组里面，选个其他分组吧", Snackbar.LENGTH_LONG).show();
                    return;
                }

                listener.onComplete(groupName);
                dismiss();
            }
        });
        //当分组 长度为0 没有分组的时候，就把显示分组的listView 隐藏掉
        if (groupList.size() == 0) {
            findViewById(R.id.ll_select_group).setVisibility(View.GONE);
        } else {
            initGroupList(groupList);
        }

    }

    private void initGroupList(final List<String> groupList) {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {

                return groupList.size();
            }

            @Override
            public Object getItem(int position) {
                return groupList.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.tx_list_item_group, null);
                }
                TextView tv = convertView.findViewById(R.id.tv_group_item);

                tv.setText(groupList.get(position));
                return convertView;
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (contact != null && TextUtils.equals(contact.getGroupName(), groupList.get(position))) {
                    Snackbar.make(rl_confirm, "已在该分组里面，选个其他分组吧", Snackbar.LENGTH_LONG).show();
                    return;
                }

                listener.onComplete(groupList.get(position));
                dismiss();
            }
        });
    }

    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    public interface InteractionListener {
        void onComplete(String newGroupName);

    }

}
